﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using OnlineManagement.Model;
using System.Data;
namespace OnlineManagement.Web.WebServices
{
    /// <summary>
    /// Summary description for HogService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    [ScriptService]
    public class HogService : System.Web.Services.WebService
    {

        [WebMethod]
        public JsonMsg HogPurchase(int hoggeryId, string pigCategory, string typeIDStr, bool isCombine, string pigPenIdStr, int phaseId, string birthdayStr, string purchaseDateStr, int account,int unitName,string unitPriceStr, string totalWightStr)
        {
            Guid pigPenId;
            Guid typeId;
            DateTime dt1;
            decimal totalWight;
            decimal unitPrice;
            DateTime? datePurchase = null;
            DateTime? birthday = null;
            JsonMsg msg;
            if (!Guid.TryParse(pigPenIdStr, out pigPenId) 
                || !Guid.TryParse(typeIDStr, out typeId)
                ||(!string.IsNullOrEmpty(purchaseDateStr)&&!DateTime.TryParse(purchaseDateStr,out dt1))
                || !decimal.TryParse(totalWightStr, out totalWight)
                ||!decimal.TryParse(unitPriceStr,out unitPrice)
                )
            {
                msg = new JsonMsg() { Flag = false,  Message =SystemMsgs.InputError };
                return msg;
            }
            if (DateTime.TryParse(birthdayStr, out dt1))
            {
                birthday = new Nullable<DateTime>(dt1);
            }
            if (DateTime.TryParse(purchaseDateStr, out dt1))
            {
                datePurchase = new Nullable<DateTime>(dt1);
            }
            Model.HogPurchase model = new HogPurchase()
                    {
                        HoggeryID = hoggeryId,
                        PigCategory = pigCategory,
                        PhaseID = phaseId,
                        PigPenId = pigPenId,
                        TypeID = typeId,
                        Birthday = birthday,
                        PurchaseDate = datePurchase,
                        TotalWeight = totalWight,
                        PurchaseAccount = account,
                        UnitPrice = unitPrice,
                        UnitName = unitName,
                        IsCombine = isCombine
                    };
            string message;
            BLL.HogPurchase bllPurchase = new BLL.HogPurchase();
            try
            {
              // bool result= bllPurchase.PurchaseHogAction(model, out message);
               bool result = true;
               message = DateTime.Now.ToString();
               msg = new JsonMsg() { Flag=result,Message=message };
            }catch(Exception){
                msg = new JsonMsg() { Flag=false,Message=SystemMsgs.SystemError};
            }

            return msg;
        }

        

    }
}
